<style lang="scss" scoped>
    @import '@/assets/css/index';
    .verification{
        @include page();
		// display:flex;
		// flex-direction: column;
		text-align: center;
		.iconImg{
			width: 140px;
			height: 140px;
			margin-top: 80px;
		}
		.result{
			margin-top: 30px;
			color: #16D17B;
			font-size: 1.6rem;
		}
		.desc{
			margin-top: 10px;
			color:$color2;
			.text{
				color: #16D17B;
			}
		}
		.btnBox{
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 0 1.6rem;
			margin-top: 4rem;
			.btn1{
				flex: 1;
				margin-right: 1rem;
			}
			.btn2{
				flex: 1;
				margin-left: 1rem;
			}
		}
    }
</style>
<template>
  <div class="verification">
	  <div v-if="result==1">
		  <img src="../../assets/img/success.png" class="iconImg">
		  <div>
			  <div class="result">签到成功</div>
			  <div class="desc">恭喜您，获得<span class="text">{{integral}}</span>积分</div>
		  </div>
	  </div>
	  <div v-else-if="result==0">
		  <img src="../../assets/img/fail.png" class="iconImg">
		  <div>
				<div class="result">签到失败</div>
				<div class="desc">请重新扫码签到或联系管理员</div>
		  </div>
	  </div>
	  <div class="btnBox">
		  <div class="btn1">
			  <van-button round plain type="info" style="width: 100%;" @click="toView('/home')">返回首页</van-button>

		  </div>
		  <div class="btn2">
			<van-button round block type="info" style="width: 100%;" @click="toView('/my/jifen')">查看积分</van-button>
		  </div>
	  </div>
	  <!-- <div>openid:{{openid}}</div>
	  <div>params:{{params}}</div>
	  <div>record:{{record}}</div> -->
  </div>
</template>

<script>
import {Toast} from 'vant'
export default {
    name: 'saomaqiandao',
    data () {
        return {
            // activity_id:'',
            openid:localStorage.getItem('openid'),
            order_id:'',
			record:'',
			integral:'',
			result: 2
        }
    },
    mounted () {
        const _this = this
        // _this.activity_id = _this.$route.params.activity_id;
        // _this.openid = _this.$route.query.openid;
        // _this.order_id = _this.$route.query.order_id;
		Toast.loading({
		  message: '加载中...',
		  forbidClick: true,
		});
        _this.getWeixinConfig()
    },
    methods: {
		toView(path){
			this.$router.replace(path)
		},
        
		getWeixinConfig() {
		  const _this = this;
		  let params = {}
		  _this.$request.getWeixinConfig(params).then(res=>{
			  // alert("getWeixinConfig:"+JSON.stringify(res))
		    _this.$store.commit('setAppid', res.data.value.appid)
		    _this.$store.commit('setSecret', res.data.value.secret)
		    _this.getCode(res.data.value.appid)
		    // if (process.env.NODE_ENV == 'production') {
		    //   _this.getCode(res.data.value.appid)
		    // }
		  })
		},
		getCode (appid) { // 非静默授权，第一次有弹框
		  const _this = this;
		  const code = this.getUrlParam('code') // 截取路径中的code，如果没有就去微信授权，如果已经获取到了就直接传code给后台获取openId
		  const local = window.location.href
		  if (code == null || code === '') {
		    window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='+ appid +'&redirect_uri=' + encodeURIComponent(local) + '&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect'
		  } else {
		    _this.$store.commit('setCode', code)
			// alert(('setCode:'+code))
		    _this.getOpenId(code) //把code传给后台获取用户信息
		  }
		},
		getUrlParam(name) {
		  var reg=new RegExp('(^|&)'+name+'=([^&]*)(&|$)')
		  var r=window.location.search.substr(1).match(reg)
		  if(r!=null) return unescape(r[2])
		  return null
		},
		getOpenId(code) {
		  const _this = this;
		  let params = {
		      appid: _this.$store.state.appid,
		      secret: _this.$store.state.secret,
		      code: code
		  }
		  // alert("params:"+JSON.stringify(params))
		  _this.$request.getOpenid(params).then(res=>{
			  // alert("getOpenid:"+JSON.stringify(res))
		      const userInfo = res.data[0];
		      const openid = userInfo["openid"];
		      // _this.$store.commit('setOpenid', openid)
			  if(localStorage.getItem('openid')) {
				  
			  }else{
				this.openid = openid
				localStorage.setItem('openid', openid)
			  }
			  _this.MembersignH5Saves(this.openid)
		  })
		},
		// 签到加积分记录
		IntegralSignIntegral(openid) {
			let params = {
			    openid: openid,
				course_id: this.$route.query.course_id,
				campus_id:JSON.parse(localStorage.getItem('selectedCampus')).id
			}
			this.params = JSON.stringify(params)
			this.$request.IntegralSignIntegral(params).then(res=>{
				this.integral = res.data.integral
				this.record = JSON.stringify(res)
			}).catch(res=>{
				this.record = JSON.stringify(res)
			})
		},
		// 签到加积分接口
		MembersignH5Saves(openid) {
			const _this = this
		    let params = {
		        openid,
		        course_id: this.$route.query.course_id,
				menu_id: this.$route.query.zhang_id,
				start_time: this.$route.query.start_time,
				end_time: this.$route.query.end_time,
				campus_id: JSON.parse(localStorage.getItem('selectedCampus')).id || 1
		    }
			// alert("params:"+JSON.stringify(params))
		    this.$request.MembersignH5Saves(params).then(res=>{
		        if(res.ret == 400) {
		            this.result = 0
		        }else if(res.ret == 200) {
					this.result = 1
		            _this.IntegralSignIntegral(this.openid)
		        }
		    }).finally(()=>{
				Toast.clear()
			})
		},
    }
}
</script>